package com.shenfa.action.tree;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.shenfa.bean.Person;
import com.shenfa.bean.ProductInfType;
import com.shenfa.bean.TreeNode;
import com.shenfa.service.CityService;
import com.shenfa.service.ProductService;
import com.shenfa.service.systemManager.SystemService;
import com.shenfa.service.systemManager.organization.OrganizationService;
import com.shenfa.service.systemManager.role.RoleService;

public class TreeAction extends ActionSupport implements ServletRequestAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String node;
	
	private List<TreeNode> nodes;
	
	private static Logger log=Logger.getLogger(TreeAction.class);
	
	@Autowired
	private OrganizationService organizationService;
	@Autowired
	private ProductService productService;
	@Autowired
	private CityService cityService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private SystemService systemService;
	
	private HttpServletRequest request;
	
	/**
	 * 机构树
	 * @return
	 */
	public String organization(){
		nodes=new ArrayList<TreeNode>();
		Person currentPerson=(Person) request.getSession().getAttribute("login");
		if (null != currentPerson){
			String s_depno =currentPerson.getDepNo();
			if("root".equals(node)){
				nodes=organizationService.getTree_bydepNo(s_depno);
			}else{
				nodes=organizationService.getTree(node);
			}
			
		}
		return SUCCESS;
	}
	/**
	 * 生成产品树
	 * 
	 * @author longchensan create by 2011-8-25
	 *
	 * @return 
	 */
	public String product(){
		log.debug("node="+node);
		nodes=new ArrayList<TreeNode>();
		if("root".equals(node)){
			ProductInfType product = new ProductInfType();
			nodes=productService.doTreeRoot(product);
		}else{
			TreeNode treenode = new TreeNode();
			treenode.setId(node);
			nodes=productService.doTreeMenu(treenode);
		}
		return SUCCESS;
	}
	
	/**
	 * 城市树
	 * 
	 * @author yanglong
	 * 
	 * @date 2011-8-25
	 *
	 * @return 
	 */
	public String city(){
		log.debug("node="+node);
		nodes=new ArrayList<TreeNode>();

		if("root".equals(node)){
			nodes=cityService.queryTree("0");
		}else{
			nodes=cityService.queryTree(node);
		}
		return SUCCESS;
	}
	
	/**
	 * 功能列表树
	 * 
	 * @author yanglong
	 */
	public String function() {
		nodes=new ArrayList<TreeNode>();
		log.debug("node="+node);
		if("root".equals(node)){
			nodes=systemService.getTree("0");
		}else{
			nodes=systemService.getTree(node);
		}
		return SUCCESS;
	}
	/**
	 * 角色管理的树
	 * @return
	 */
	public String role(){
		nodes=new ArrayList<TreeNode>();
		log.debug("node="+node);
		if("root".equals(node)){
			Person currentPerson=(Person) request.getSession().getAttribute("login");
			nodes=roleService.getChildTree(currentPerson.getOrgNo());
		}else{
			nodes=roleService.getTree(node);
		}
		return SUCCESS;
	}
	
	public List<TreeNode> getNodes() {
		return nodes;
	}

	public void setNode(String node) {
		this.node = node;
	}
	public void setServletRequest(HttpServletRequest arg0) {
		this.request=arg0;
	}
}
